﻿using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using PlanetTelex.SeoMetaTags.Models;

namespace PlanetTelex.SeoMetaTags.Drivers
{
    public class SeoMetaTagsDriver : ContentPartDriver<SeoMetaTagPart>
    {
        /// <summary>
        /// The Display Driver
        /// </summary>
        /// <param name="part"></param>
        /// <param name="displayType"></param>
        /// <param name="shapeHelper"></param>
        /// <returns></returns>
        protected override DriverResult Display(SeoMetaTagPart part, string displayType, dynamic shapeHelper)
        {
            return ContentShape("Parts_SeoMetaTags", () => shapeHelper.DisplayTemplate(TemplateName: "Parts/SeoMetaTags", Model: part, Prefix: Prefix));

        }

        /// <summary>
        /// The Editor GET Driver
        /// </summary>
        /// <param name="part"></param>
        /// <param name="shapeHelper"></param>
        /// <returns></returns>
        protected override DriverResult Editor(SeoMetaTagPart part, dynamic shapeHelper)
        {
            return ContentShape("Parts_SeoMetaTags_Edit", () => shapeHelper.EditorTemplate(TemplateName: "Parts/SeoMetaTags", Model: part, Prefix: Prefix));
        }

        /// <summary>
        /// The Editor POST Driver.
        /// </summary>
        /// <param name="part"></param>
        /// <param name="updater"></param>
        /// <param name="shapeHelper"></param>
        /// <returns></returns>
        protected override DriverResult Editor(SeoMetaTagPart part, IUpdateModel updater, dynamic shapeHelper)
        {
            updater.TryUpdateModel(part, Prefix, null, null);
            return Editor(part, shapeHelper);
        }

        /// <summary>
        /// The Import Driver.
        /// </summary>
        /// <param name="part">The part.</param>
        /// <param name="context">The context.</param>
        protected override void Importing(SeoMetaTagPart part, ImportContentContext context)
        {
            var pageTitle = context.Attribute(part.PartDefinition.Name, "PageTitle");
            var metaDescription = context.Attribute(part.PartDefinition.Name, "MetaDescription");
            var metaKeywords = context.Attribute(part.PartDefinition.Name, "MetaKeywords");

            if (!string.IsNullOrEmpty(pageTitle)) part.PageTitle = pageTitle;
            if (!string.IsNullOrEmpty(metaDescription)) part.MetaDescription = metaDescription;
            if (!string.IsNullOrEmpty(metaKeywords)) part.MetaKeywords = metaKeywords;
        }

        /// <summary>
        /// The Export Driver.
        /// </summary>
        /// <param name="part">The part.</param>
        /// <param name="context">The context.</param>
        protected override void Exporting(SeoMetaTagPart part, ExportContentContext context)
        {
            context.Element(part.PartDefinition.Name).SetAttributeValue("PageTitle", part.PageTitle);
            context.Element(part.PartDefinition.Name).SetAttributeValue("MetaDescription", part.MetaDescription);
            context.Element(part.PartDefinition.Name).SetAttributeValue("MetaKeywords", part.MetaKeywords);
        }
    }
}